var formatTime = function (strDate, format = "YYYY-MM-DD hh:mm:ss") {
  if (strDate == null || strDate == undefined || strDate == "") {
    return "";
  }
  console.log(strDate);
  // 解决ios出现NaN问题
  strDate = strDate.replace("T", " ");
  var realDate = strDate ? getDate(strDate.replace(getRegExp('-', 'g'), '/')) : getDate();
  var regYear = getRegExp("(y+)", "i");
  var date = [["M+", realDate.getMonth() + 1], ["D+", realDate.getDate()], ["h+", realDate.getHours()], ["m+", realDate.getMinutes()], ["s+", realDate.getSeconds()], ["q+", Math.floor((realDate.getMonth() + 3) / 3)], ["S+", realDate.getMilliseconds()]];
  var reg1 = regYear.exec(format);
  if (reg1) {
    format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length));
  }
  for (var i = 0; i < date.length; i++) {
    var reg2 = getRegExp("(" + date[i][0] + ")").exec(format);
    if (reg2) {
      format = format.replace(reg2[1], reg2[1].length == 1 ? v : ("00" + date[i][1]).substring(("" + date[i][1]).length));
    }
  }
  return format;
};
var time = {
  TimeToDate: function (date, format) {
    format = format || 'YYYY-MM-DD hh:mm:ss';
    var dateTest = getRegExp('^(-)?\d{1,10}$').test(date) || getRegExp('^(-)?\d{1,13}$').test(date);
    if (getRegExp('^[1-9]*[1-9][0-9]*$').test(date) && dateTest) {
      var vdate = parseInt(date);
      if (getRegExp('^(-)?\d{1,10}$').test(vdate)) {
        vdate = vdate * 1000;
      } else if (getRegExp('^(-)?\d{1,13}$').test(vdate)) {
        //vdate = vdate * 1000;
        vdate = vdate;
      } else if (getRegExp('^(-)?\d{1,14}$').test(vdate)) {
        vdate = vdate * 100;
      } else {
        alert("时间戳格式不正确");
        return;
      }
      var setdate = getDate(vdate);
      return parse({
        YYYY: setdate.getFullYear(),
        MM: digit(setdate.getMonth() + 1),
        DD: digit(setdate.getDate()),
        hh: digit(setdate.getHours()),
        mm: digit(setdate.getMinutes()),
        ss: digit(setdate.getSeconds())
      }, format);
    } else {
      //将日期转换成时间戳
      re = getRegExp('(\d{4})(?:\D?(\d{1,2})(?:\D?(\d{1,2}))?[^\d\s]?)?(?:\s+(\d{1,2})\D?(\d{1,2})\D?(\d{1,2}))?').exec(date);
      return !re ? 0 : getDate(re[1], (re[2] || 1) - 1, re[3] || 1, re[4] || 0, re[5] || 0, re[6] || 0).getTime() / 1000;
    }
    function parse(ymdhms, format) {
      var regymdzz = "YYYY|MM|DD|hh|mm|ss|zz";
      return format.replace(getRegExp(regymdzz, "g"), function (str, index) {
        return str == "zz" ? "00" : digit(ymdhms[str]);
      });
    }
    function digit(num) {
      return num < 10 ? "0" + (num | 0) : num;
    }
  }
};
var formatNumber = function (n) {
  if (n < 10) {
    return '0' + n;
  }
  return n;
};
module.exports = {
  formatNumber: formatNumber,
  FormatTime: formatTime,
  TimeToDate: time.TimeToDate
};

//小于10的补零操作
function add(m) {
  return m < 10 ? '0' + m : m;
}